package sg.edu.nus.iss.billing.service.bill;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.dao.bill.PaymentHistoryDAO;
import sg.edu.nus.iss.billing.vo.bill.PaymentVO;

public class PaymentImpl implements IPayment {

	@Override
	public List<PaymentVO> getLastPayment(String accountNo,
			Date currentDate) {
		List<PaymentVO> paymentList= null;
		PaymentHistoryDAO paymentHistoryDAO= new PaymentHistoryDAO();
		Date lastBillDate = Util.dateAddMonth(currentDate, -1);
		paymentList=paymentHistoryDAO.getPaymentMade(accountNo, lastBillDate, currentDate);
		return paymentList;
	}

	@Override
	public double getLastPaymentTotalAmt(String accountNo, 
			Date currentDate) {
		List<PaymentVO> paymentList= null;
		double paymentTotalAmt = 0;
		Date lastBillDate = Util.dateAddMonth(currentDate, -1);
		PaymentHistoryDAO paymentHistoryDAO= new PaymentHistoryDAO();
		
		System.out.println("lastBillDate"+lastBillDate);
		System.out.println("currentDate"+currentDate);
		
		paymentList=paymentHistoryDAO.getPaymentMade(accountNo, lastBillDate, currentDate);
		System.out.println("accountNo "+accountNo +"paymentList"+paymentList.size());
		if(paymentList != null)
		{
			 for(Iterator<PaymentVO> payments = paymentList.iterator(); payments.hasNext(); ) 
			 {
					//Subscription Main Plan
				 PaymentVO payment = payments.next();

				 System.out.println("payment.getPaymentDate()"+payment.getPaymentDate());
				 
				 if (Util.isBetweenDate(lastBillDate, currentDate, payment.getPaymentDate()))
				 {
					 paymentTotalAmt += payment.getPaymentMade();
					 System.out.println("payment.getPaymentMade()"+payment.getPaymentMade());
				 }
			 }
			 System.out.println("paymentTotalAmt"+paymentTotalAmt);
			 return paymentTotalAmt;
		}
		System.out.println("Payment List is Null");
		return 0;
	}

}
